package com.gaohe.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gaohe.common.BaseContext;
import com.gaohe.common.R;
import com.gaohe.pojo.Card;
import com.gaohe.pojo.Classes;
import com.gaohe.pojo.UserClass;
import com.gaohe.service.ClassesService;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

@RestController
@Slf4j
@RequestMapping("/classes")
@CrossOrigin
public class ClassesController {
    @Autowired
    ClassesService classesService;

    @Autowired
    RedisTemplate redisTemplate;

//    删除课程
    @DeleteMapping("/delete")
    public R changeStatusTo1(Long id)
    {
        Classes classes = classesService.getById(id);
        classes.setUpdateTime(LocalDateTime.now());
        classes.setIsDeleted(1);
        boolean flag = classesService.updateById(classes);
        if(flag)
        {
            return R.success("删除成功");
        }else{
            return R.error("删除失败");
        }
    }

//    恢复课程
    @DeleteMapping("/restore")
    public R changeStatusTo0(Long id)
    {
        Classes classes = classesService.getById(id);
        classes.setIsDeleted(0);
        return classesService.updateById(classes)? R.success("恢复成功"):R.error("恢复失败");
    }

//    新增卡
   @PostMapping
   public R save(@RequestBody Classes classes, HttpSession httpSession)
   {
       classes.setStatus(1);
       classes.setCreateTime(LocalDateTime.now());
       classes.setUpdateTime(LocalDateTime.now());
//       Long user = (Long)httpSession.getAttribute("user");
       Long user = (Long)redisTemplate.opsForValue().get("auth");
       classes.setUpdateUser(user);
       classes.setCreateUser(user);
       boolean flag = classesService.save(classes);
       return flag?R.success("新增成功"):R.error("新增失败");
   }

//     分页查询
    @GetMapping("/page")
    public R page(@RequestParam Integer page, @RequestParam Integer pageSize, @RequestParam(required = false) String name)
    {
//        Page是一个ArrayList实现，存储查询结果，PageInfo则包含更多分页信息如导航页码。
//        使用Page还是PageInfo取决于前端需求，通常PageInfo更适合提供详细的分页数据。
        Page<Classes> page1 = new Page<>(page,pageSize);
        LambdaQueryWrapper<Classes> qw = new LambdaQueryWrapper<>();
        qw.like(!StringUtils.isEmpty(name),Classes::getName,name);
        qw.orderByDesc(Classes::getUpdateTime);
        qw.orderByAsc(Classes::getName);
        return R.success(classesService.page(page1, qw));
    }

//    分页查询已被删除的数据1
    @GetMapping("/page1")
    public R<Page>page1(@RequestParam Integer page, @RequestParam Integer pageSize, @RequestParam( required = false) String name)
    {
        Page<Classes>page1 = new Page<>(page,pageSize);
        LambdaQueryWrapper<Classes>qw = new LambdaQueryWrapper<>();
        qw.like(!StringUtils.isEmpty(name),Classes::getName,name);
        qw.eq(Classes::getIsDeleted,1);
        qw.orderByDesc(Classes::getUpdateTime);
        qw.orderByAsc(Classes::getName);
        return R.success(classesService.page(page1,qw));
    }

    //    获取id
    @GetMapping("{id}")
    private R getId(@PathVariable Long id)
    {
        return R.success(classesService.getById(id));
    }

//     编辑员工信息
    @PutMapping
    public R update(@RequestBody Classes classes)
    {
        classes.setUpdateTime(LocalDateTime.now());
        boolean flag = classesService.updateById(classes);
        return flag?R.success("编辑成功"):R.error("编辑失败");
    }


//     选择员工任职状态
    @PutMapping("/changeStatus")
    public R changeStatus(@RequestBody Classes classes, HttpSession session)
    {
//        (Long) session.getAttribute("user")
        classes.setUpdateUser((Long)redisTemplate.opsForValue().get("auth"));
        classes.setUpdateTime(LocalDateTime.now());
        boolean flag = classesService.updateById(classes);
        return flag?R.success("修改成功"):R.error("修改失败");
    }

//      通过会员卡标签查询适用课程 用户端
    @GetMapping("/getClassByCardTags")
    public R getClassByCardTags(@RequestParam String tags){

        log.info("tags:{}",tags);
        List<String> list = classesService.getClassByCardTags(tags);
        return R.success(list);
    }

//    根据标签名称获取课程列表
    @GetMapping("/getClassList/{tag}")
    public R getClassListByTag(@PathVariable String tag)
    {
        List<Classes>list = classesService.getClassListByTag(tag);
        return R.success(list);
    }
}
